package com.comtrade.map;

import java.util.Vector;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import com.comtrade.mathematics.BeaconRacun;
/*
 * TouchView showing map and user locaton
 *
 */
public class TouchView extends View{

    private Drawable mMap;
    private Drawable mBeacon;
    private static float MIN_ZOOM = 1f;
    private static float MAX_ZOOM = 5f;
    private float scaleFactor = 1.f;
    private ScaleGestureDetector detector;
    private GestureDetector simpledetector;
    Matrix matrix;
    int height;
    int width;
    float ratio1;
    float ratio2;
    float scaleratio;
   
    public float getScaleratio() {
		return scaleratio;
	}

	public void setScaleratio(float scaleratio) {
		this.scaleratio = scaleratio;
	}

	public DotView mDot;

    private Vector<BeaconView> beaconPositions;
	private Rect clipBound;

    public TouchView(Context context) {
        this(context, null, 0, new DotView(context));
       
    }

    public TouchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0, new DotView(context));
    }

    public TouchView(Context context, DotView dot){
        this(context, null, 0, dot);
       
    }

    public Matrix getMatrix() {
		return matrix;
	}

	public void setMatrix(Matrix matrix) {
		this.matrix = matrix;
	}

	/**
     * Creates instance of touch view.
     * It needs dotView to know that it exists
     *
     * @param context
     * @param attrs
     * @param defStyle
     * @param dot
     */

    public TouchView(Context context, AttributeSet attrs, int defStyle, DotView dot) {
        super(context, attrs, defStyle);
        //setBackgroundResource(R.drawable.plan);
        detector = new ScaleGestureDetector(getContext(), new ScaleListener());
        simpledetector=new GestureDetector(getContext(), simpleOnGestureListener);
        //mMap=getResources().getDrawable(R.drawable.plan);
        matrix=new Matrix();
        mDot=new DotView(context,this);
        setDotCoordinates(50, 50);
        mDot.setH(50);
        mDot.setW(30);
       
       
    }

    /**
     * Sets Drawable map as map image
     * sets top left corner in (0, 0)
     * and bottom right cornet to (w, h)
     * @param map
     * @param w 
     * @param h 
     */
    public void setMap(Drawable map, int w, int h) {
        mMap = map;
        mMap.setBounds(0, 0, w, h);
       
        //getResizedBitmap(mMap, height, width);
       
       
    }
   
   
    public void getResizedBitmap(Drawable bm, int newHeight, int newWidth) {
        float width = mMap.getBounds().width();
        int height =  mMap.getBounds().height();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // Create a matrix for the manipulation
       

        // Resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // Recreate the new Bitmap
        invalidate();

       
    }

    /**
     * sets beacon positions
     * @param positions
     */
    public void setBeaconsPositions(Vector<BeaconView> positions){
        beaconPositions = positions;
    }

    /**
     * For drawing map
     */
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       
       
        canvas.save();
       
        height=getMeasuredHeight();
        width=getMeasuredWidth();
       
        ratio1=height*1.0f/mMap.getBounds().height();
        ratio2=width*1.0f/mMap.getBounds().width();
        //getResizedBitmap(mMap, height, width);
       
        scaleratio=Math.min(ratio1, ratio2);
        canvas.concat(matrix);
     
        canvas.scale(scaleratio, scaleratio);
        //mMap.setBounds(0, 0, 0, 0);
        //canvas.drawBitmap(((BitmapDrawable)mMap).getBitmap(), 0, 0, null);
        clipBound = canvas.getClipBounds();
        mMap.draw(canvas);
    //    Log.d("TACKA H",  Integer.toString(mDot.getW()));
      //  Log.d("TACKA", mDot.toString());
        mDot.draw(canvas);
        for(BeaconView b:beaconPositions){
        	b.draw(canvas);
        }
     //   Log.d("MAPA213123","Asdasdas");
        canvas.restore();     

    }


    public Drawable getmMap() {
		return mMap;
	}

	public void setmMap(Drawable mMap) {
		this.mMap = mMap;
	}

	/**
=======
    public Rect getClipBound() {
		return clipBound;
	}

	public void setClipBound(Rect clipBound) {
		this.clipBound = clipBound;
	}

	/**
>>>>>>> .r95
     * Sets coordinates for dotView to (x, y)
     * @param x
     * @param y
     */
    public void setDotCoordinates(float x, float y) {
        mDot.setCoordinates(x, y);
        invalidate();
    }

    /**
     *
     * @return mDotView
     */
    public DotView getDot(){
        return mDot;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        detector.onTouchEvent(ev);
        simpledetector.onTouchEvent(ev);
        return true;
    }

    SimpleOnGestureListener simpleOnGestureListener=new SimpleOnGestureListener(){
       
        @Override
        public boolean onDown(MotionEvent e) {
           
            return true;
        };
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            matrix.reset();
            invalidate();
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Make sure that mTextView is the text view you want to move around

            matrix.postTranslate(-distanceX, -distanceY);
              invalidate();             
            return true;
        }
    };


    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            matrix.postScale(detector.getScaleFactor(), detector.getScaleFactor(),detector.getFocusX(),detector.getFocusY());
           
            invalidate();
           
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            return super.onScaleBegin(detector);
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            super.onScaleEnd(detector);
        }
       
       
    }

	
    
    


	public void rotiraj(float currentDegree, float degree) {
		float centarX = mMap.getBounds().centerX();
		float centarY = mMap.getBounds().centerY();
			matrix.preRotate(+currentDegree-degree,centarX,centarY);
			invalidate();			
	}


}